/*! 
 * Ground - Implementation file.
 *
 * \author Devis Rossini
 * \date 02 April 2012
 */
#include "PhxOgreGround.h"

using namespace GameObjects;

//-------------------------------------------------------------------------------------
Ground::Ground(phx::Scene* iScene, Ogre::SceneManager* iSceneManager, Ogre::SceneNode* iParentNode, 
	const Ogre::Vector3& iGroundNormal, const float iGroundOffset)
{
	// Create the physical object.
	mPhxEntity = CreatePhxEntity(	iScene, 
									PhxOgreUtils::PhxVector3(iGroundNormal), 
									iGroundOffset);

	// Create the Ogre's entity associated to the ball and insert it into the scenegraph.
	mSceneNode = CreateOgreEntity(	iSceneManager, 
									iParentNode, 
									iGroundNormal, 
									iGroundOffset);
}

//-------------------------------------------------------------------------------------
phx::CollisionPrimitive* Ground::CreatePhxEntity(phx::Scene* iScene, const phx::Vector3& iGroundNormal, const phx::real iGroundOffset)
{	
	phx::CollisionPlaneDescriptor desc;
	desc.mDirection = iGroundNormal;
	desc.mOffset	= iGroundOffset;

	return iScene->CreatePrimitive(desc);
}

//-------------------------------------------------------------------------------------
Ogre::SceneNode* Ground::CreateOgreEntity(Ogre::SceneManager* iSceneManager, Ogre::SceneNode* iParentNode, 
	const Ogre::Vector3& iGroundNormal, const float iGroundOffset)
{
	// Create the plane.
	Ogre::Plane ground(iGroundNormal, iGroundOffset);

	Ogre::MeshManager::getSingleton().createPlane("Ground", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, 
		ground, 1000, 1000, 1, 1, true, 1, 1, 1, Ogre::Vector3::UNIT_X);

	// Create the entity.
	Ogre::Entity* groundEntity = iSceneManager->createEntity("GroundEntity", "Ground");
	groundEntity->setMaterialName("ground-from-nxogre.org");
	groundEntity->setCastShadows(false);

	// Create a new node and attach the plane to it.
	Ogre::SceneNode* groundNode = iParentNode->createChildSceneNode("GroundNode");
	iParentNode->attachObject(groundEntity);

	return groundNode;
}